/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
    public:
        vector<int> preorderTraversal(TreeNode* root) {
            vector<int> v;
            _preorderTraversal(root,v);
            return v;
        }
        void _preorderTraversal(TreeNode* root,vector<int>& v){
            if(root == nullptr) return;
            v.push_back(root->val);
            _preorderTraversal(root->left,v);
            _preorderTraversal(root->right,v);
            return ;
        }
    };
    
    //_preorder 的任务：前序遍历二叉树
    //子问题：将跟节点push进vector，再前序遍历左子树和右子树
    //注意出口